/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

function getEmail(name)
{
    name = name.toLowerCase().trim();
    
    name = name.replace(/à|á|ả|ã|ạ|â|ầ|ấ|ẫ|ẩ|ậ|ă|ắ|ằ|ẳ|ẵ|ặ/g, "a");
    name = name.replace(/è|é|ẻ|ẽ|ẹ|ê|ề|ế|ể|ễ|ệ/g, "e");
    name = name.replace(/ì|í|ĩ|ỉ|ị/g, "i");
    name = name.replace(/ò|ó|ỏ|õ|ọ|ô|ồ|ố|ổ|ỗ|ộ|ơ|ờ|ớ|ở|ỡ|ợ/g, "o");
    name = name.replace(/ù|ú|ủ|ũ|ụ|ư|ừ|ứ|ử|ữ|ự/g, "u");
    name = name.replace(/ỳ|ý|ỷ|ỹ|ỵ/g, "y");    
    name = name.replace(/\s\s+/g, " ");
    
    names = name.split(" ");
    
    var length = names.length;
    var number = parseInt(names[length - 1]);
    
    if (number) {
    	length--;
    }
    
    email = names[length - 1] + ".";
    for (var i = 0; i < length - 1; i++) {
    	email += names[i];
    }
    
    if (number) {
    	email += number;
    }
    
    return email;
}

function switchLayout()
{
    if ($(document.body).hasClass('hideleft')) {
        $(document.body).removeClass('hideleft');
    } else {
        $(document.body).addClass('hideleft');
    }
}

$(document).ready(function(){
    normalize(document.body);    
});

function toogleClass($this, className)
{
	if ($($this).hasClass("active"))
	{
		$($this).removeClass("active");
		$(document.body).addClass(className);
	}
	else
	{
		$($this).addClass("active");
		$(document.body).removeClass(className);
	}
}

function normalize(item) {
    $(item).find(".editor").hallo({
        plugins: {
          'halloformat': {},
          'halloblock': {},
          'hallojustify': {},
          'hallolists': {},
          'halloreundo': {}
        }
      });
      
    $(item).find(".combobox").each(function (index, element) {
        if (!element.dropdown) {
            element.dropdown = true;
            new DropDown($(element));
        }
    });
     
    $(item).find(".grid-view").gridView();
    $(item).find(".editor").paste();

    $(item).find(".datepicker").datepick();
    
    $(item).find(".blur-on-enter").keypress(function(event){
        if (event.keyCode === 13) {
            $(this).blur();
            return false;
        } else {
            return true;
        }
   });
   
   $(item).find(".blank-on-focus").focus(function(){
       $(this).html("");
   });
   
   $(item).find(".cell-editor").keydown(function(event) {
       if (event.keyCode === 38) {
           var cellEditors = $(".cell-editor");
           var index = indexOf(cellEditors, this);
           if (index > 0) {
               $(this).blur();
               $(cellEditors[index - 1]).focus();
           }
       } else if (event.keyCode === 40 || event.keyCode === 9) {
           var cellEditors = $(".cell-editor");
           var index = indexOf(cellEditors, this);
           if (index < cellEditors.length - 1) {
               $(this).blur();
               $(cellEditors[index + 1]).focus();
           }
       }
   });
   
   $(item).find(".rating").each(function(index, element){        
        if (!element.childrens) {
            element.value = parseInt(element.innerHTML);
            element.innerHTML = "";
        
            element.childrens = new Array();
            for (var i = 0; i < 5; i++) {
                element.childrens[i] = $("<a>&nbsp;&nbsp;</a>").appendTo(element);
                element.childrens[i][0].index = i;
                element.childrens[i][0].parent = element;
                
                if (!$(element).hasClass("disabled")) {
                    $(element.childrens[i]).click(function() {
                        if (this.parent.value !== (this.index + 1)) {
                            this.parent.setValue(this.index + 1);
                        } else {
                            this.parent.setValue(0);
                        }

                        if (this.parent.onchange) {
                            this.parent.onchange();
                        }
                    });
                }
            }
            
            
            
            element.setValue = function(value) {
                this.value = value;
                $(this).find("a").removeClass("checked");
                if (value > 0) {
                    $(this).removeClass("unrated");
                    $(this.childrens[element.value - 1]).addClass("checked");
                } else {
                    $(this).addClass("unrated");
                }
            };
            
            element.setValue(element.value);
        }
   });

}


function indexOf(array, element) {
   var result = 0;
   for (var i = 0; i < array.length; i++){
       if (array[i] === element) {
           result = i;
       }
   }
   return result;
}

function updateCounter() {
    $("div.counter").each(function(index, element) {
        element.innerHTML = "#" + (index + 1);
    });
}